#include "StdAfx.h"
#include "MVCodec.h"
#include "TG_Data_Def.h"

MVCodec::MVCodec(void)
{
}

MVCodec::~MVCodec(void)
{
}

int MVCodec::Create(TG_MV_SRC *pMVSrc,void **ppFrame,long &lFrameSize)
{
	*ppFrame   = NULL;
	lFrameSize = 0;

	/*
	if( NULL == pMVSrc->pInput ||
		pMVSrc->lInSize > ( 20*1024 - sizeof(TG_FRAME_HEAD) - sizeof(TG_NAL_HEAD) - 6)  )
	{
		return -1;
	}

	TG_FRAME_HEAD *pFrameHead = (TG_FRAME_HEAD*)m_OutBuf;

	pFrameHead->lFrameFlag = TG_FRAME_HEAD_FLAG;
	pFrameHead->nFrameType = pMVSrc->lFrameType;
	pFrameHead->nHeadSize  = sizeof(TG_FRAME_HEAD);
	pFrameHead->lFrameSize = sizeof(TG_NAL_HEAD) + 6 + pMVSrc->lInSize + pMVSrc->lInSizeEx;
	pFrameHead->lTimeStamp = pMVSrc->lTimeStamp;
	pFrameHead->lEncodeType = 0;	

	TG_NAL_HEAD *pNalHead = (TG_NAL_HEAD *)( m_OutBuf + sizeof(TG_FRAME_HEAD) );
	pNalHead->cNalType = (char)pMVSrc->lNalType;
	pNalHead->lNalSize = 6 + pMVSrc->lInSize + pMVSrc->lInSizeEx;

	unsigned char *pNalData = m_OutBuf + sizeof(TG_FRAME_HEAD) + sizeof(TG_NAL_HEAD);
	pNalData[0] = 0x00;
	pNalData[1] = 0x00;
	pNalData[2] = 0x00;
	pNalData[3] = 0x01;
	pNalData[4] = 0x1B;
	pNalData[5] = pNalHead->cNalType;

	pNalData += 6;
	if( NULL != pMVSrc->pInput && pMVSrc->lInSize > 0 )
	{
		memcpy(pNalData,pMVSrc->pInput,pMVSrc->lInSize);

		pNalData += pMVSrc->lInSize;
	}
	if( NULL != pMVSrc->pInputEx && pMVSrc->lInSizeEx > 0 )
	{
		memcpy(pNalData,pMVSrc->pInputEx,pMVSrc->lInSizeEx);
	}

	*ppFrame   = (void*)m_OutBuf;
	lFrameSize = pFrameHead->lFrameSize + sizeof(TG_FRAME_HEAD);
	*/
	return 0;
}

int MVCodec::PickMV(BYTE *pFrame,long lFrameSize,TG_MV_SRC *pMVSrc)
{
	if( NULL == pMVSrc || NULL == pFrame || 
		lFrameSize < ( sizeof(TG_FRAME_HEAD) + sizeof(TG_NAL_HEAD) + 6 ) )
	{
		return -1;
	}
/*
	memset(pMVSrc,0,sizeof(TG_MV_SRC));

	TG_FRAME_HEAD *pFrameHead = (TG_FRAME_HEAD*)pFrame;
	TG_NAL_HEAD *pNalHead = (TG_NAL_HEAD *)( pFrame + sizeof(TG_FRAME_HEAD) );

	pMVSrc->lTimeStamp  = pFrameHead->lTimeStamp;
	pMVSrc->lFrameType  = pFrameHead->nFrameType;
	pMVSrc->lNalType    = pNalHead->cNalType;	

	pMVSrc->lInSize     = pNalHead->lNalSize - 6;
	pMVSrc->pInput      = pFrame + sizeof(TG_FRAME_HEAD) + sizeof(TG_NAL_HEAD) + 6;

	if( TG_FRAME_TYPE_MV_D   == pFrameHead->nFrameType ||
		TG_FRAME_TYPE_MV_D2  == pFrameHead->nFrameType    )
	{
		pMVSrc->lInSize     = sizeof(TG_MV_INFO);//20; //pNalHead->lNalSize - 6;
		pMVSrc->pInput      = pFrame + sizeof(TG_FRAME_HEAD) + sizeof(TG_NAL_HEAD) + 6;

		pMVSrc->lInSizeEx   = pNalHead->lNalSize - 6 - sizeof(TG_MV_INFO);;
		pMVSrc->pInputEx    = pFrame + sizeof(TG_FRAME_HEAD) + sizeof(TG_NAL_HEAD) + 6 + sizeof(TG_MV_INFO);;
	}
*/
	return 0;
}